package cn.noerdenfit;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestMain {

    public static void main(String[] args) throws IOException {
        File file = new File("F:\\User\\workspace\\I18nDemo\\i18nResource\\test.xml");
        String oriContent = FileUtils.readFileToString(file, "utf-8");

        String fileContent = oriContent;
//        fileContent = fileContent.replaceAll("([^\\\\])\\\\n", "$1\r\n");

//        fileContent = fileContent.replaceAll("\\s*<!\\[CDATA\\[([\\s\\S]*?)]]>\\s*", "$1".trim().replaceAll("\r\n", ""));

//        fileContent = fileContent.replaceAll("([^\\\\])\\\\([^\\\\])", "$1$2");

//        fileContent = fileContent.replaceAll("(<string .*?)>(\\s*)(</string>)", "$1/>");
        fileContent = fileContent.replaceAll("(<string.*?\".*?\"\\s*)>\\s*</string>", "$1/>");

//        fileContent = fileContent.replaceAll("([^(resources|string)]>)(\\s*)(.*?)(\\s*)(</string>)", "$1$3$5");

//        String newContent = clearLineBreak(oriContent);
//
//        String fileContent = newContent;
//        String tContent = newContent;
//
//        fileContent = doHtml(fileContent, tContent);
//        fileContent = doXX(fileContent);
//        fileContent = clearEmptyStrings(fileContent);
//        fileContent = clearRedundancyBlank(fileContent);

        File dstFIle = new File(file.getParent(), "testcopy.xml");
        FileUtils.write(dstFIle, fileContent, "utf-8", false);
    }

    private static String clearLineBreak(String oriContent) {
        return oriContent.replaceAll("([^\\\\])\\\\n", "$1\r\n");

//        final String FlAG = "TclearLineBreak" + System.currentTimeMillis();
//        oriContent = oriContent.replaceAll("\\\\\\\\", FlAG);
//        oriContent = oriContent.replaceAll("\\\\n", "\r\n");
//        oriContent = oriContent.replaceAll(FlAG, "\\\\");
//
//        return oriContent;
    }


    private static String clearRedundancyBlank(String newContent) {
        final String FLAG_S1 = "TclearRedundancyBlank" + System.currentTimeMillis();
        String regex;
        Pattern pattern;
        Matcher matcher;

        String tContent = newContent;
        regex = "[^s/]>(\\s*).*?(\\s*)</string>";

        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(tContent);
        StringBuffer sb = new StringBuffer("");
        while (matcher.find()) {
            String item = matcher.group();

            item = item.replaceAll("\\$", FLAG_S1);
            item = item.replaceAll(matcher.group(1), "");
            item = item.replaceAll(matcher.group(2), "");

//            LogHelper.info("item=%s", item);
//            newContent = matcher.replaceAll(item);
            matcher.appendReplacement(sb, item);
        }

        matcher.appendTail(sb);
        newContent = sb.toString();
        newContent = newContent.replaceAll(FLAG_S1, "\\$");

        return newContent;
    }

    private static String clearEmptyStrings(String newContent) {
        String regex;
        Pattern pattern;
        Matcher matcher;

        String tContent = newContent;
        regex = ">\\s*?</string>";

        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(tContent);
        while (matcher.find()) {
            String item = matcher.group();
            newContent = matcher.replaceAll("/>");
        }

        return newContent;
    }

    private static String doXX(String fileContent) {
        final String FLAG = "_" + System.currentTimeMillis();
        String tContent;
        String ragex;
        Pattern p;
        Matcher m;
        tContent = fileContent;

        tContent = tContent.replaceAll("\\\\\\\\", FLAG);

        ragex = "\\\\([^\\\\])";
        p = Pattern.compile(ragex);
        m = p.matcher(tContent);
        while (m.find()) {
            String s = m.group(1);
            System.out.println(s);

            System.out.println(s);
            fileContent = m.replaceAll(s);

            System.out.println(fileContent);
        }

        fileContent = fileContent.replaceAll(FLAG, "\\\\\\\\");

        return fileContent;
    }

    private static String doHtml(String fileContent, String tContent) {
        final String FLAG_S1 = "T1" + System.currentTimeMillis();
//        final String FLAG_S2 = "T2" + System.currentTimeMillis() + 100;
//        final String S2 = "%";

        String ragex;
        Pattern p;
        Matcher m;
        ragex = "\\s*<!\\[CDATA\\[([\\s\\S]*?)]]>\\s*";
        p = Pattern.compile(ragex);
        m = p.matcher(tContent);

        while (m.find()) {
            LogHelper.info("cnt=%d", m.groupCount());
            String s = m.group();
            System.out.println(s);


            s = m.group(1).trim().replaceAll("\r\n", "");
            s = s.replaceAll("\\$", FLAG_S1);

            System.out.println("g1=" + s);
            fileContent = m.replaceAll(s);
//            System.out.println("内容\n" + s);
//            m.replaceAll(s);
        }

        fileContent = fileContent.replaceAll(FLAG_S1, "\\$");

        return fileContent;
    }
}
